1342B - Binary Period - CodeForces Solution


constructive algorithms strings *1100

Please click on ads to support us..

Python Code:

def inverse(x):
    if x == '1':
        return '0'
    return '1'


for _ in range(int(input())):
        t = input()

    if len(set(t)) == 1:
        print(t)
        continue
    
    ans = t[0]
    for i in t[1:]:
        if i == ans[-1]:
            ans += inverse(i) + i
        else:
            ans += i
    print(ans)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        string s;
        cin>>s;
        int n=s.length();
        int z=0,o=0;
        for(int i=0;i<n;i++){
            if(s[i]=='0'){
                z++;
            }
            else{
                o++;
            }
        }
        if(o==n || z==n){
            cout<<s;
        }
        else{
            string p="10";
            string ans="";
            for(int i=1;i<=n;i++){
                ans+=p;
            }
            cout<<ans;
        }
        cout<<endl;
    }
}


Comments

Submit
0 Comments
More Questions

1278B - A and B
1353D - Constructing the Array
1269C - Long Beautiful Integer
1076A - Minimizing the String
913C - Party Lemonade
1313A - Fast Food Restaurant
681A - A Good Contest
1585F - Non-equal Neighbours
747A - Display Size
285A - Slightly Decreasing Permutations
515C - Drazil and Factorial
1151E - Number of Components
1151F - Sonya and Informatics
556A - Case of the Zeros and Ones
867A - Between the Offices
1569A - Balanced Substring
260A - Adding Digits
1698C - 3SUM Closure
1029B - Creating the Contest
1421A - XORwice
1029A - Many Equal Substrings
1675D - Vertical Paths
1271C - Shawarma Tent
805A - Fake NP
1163A - Eating Soup
787A - The Monster
807A - Is it rated
1096A - Find Divisible
1430C - Numbers on Whiteboard
1697B - Promo